package a10_动态规划;

/**
 * <p>
 * a31_打家劫舍3复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/3/14
 */
public class a31_打家劫舍3复习2 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(4,
                new TreeNode(1,
                        new TreeNode(2,
                                new TreeNode(3),
                                null),
                        null),
                null
        );
        a31_打家劫舍3复习2 test = new a31_打家劫舍3复习2();
        System.out.println(test.rob(root));
    }

    public int rob(TreeNode root) {
        int[] result = traversal(root);
        return Math.max(result[0],result[1]);
    }

    //array[0]：不偷此节点的最大金额
    //array[1]：偷此节点的最大金额

    public int[] traversal(TreeNode node){
        if (node == null) {
            return new int[]{0,0};
        }

        int[] result = new int[2];
        int[] leftArray = traversal(node.left);
        int[] rightArray = traversal(node.right);

        result[0] = Math.max(leftArray[0],leftArray[1]) + Math.max(rightArray[0],rightArray[1]);
        result[1] = leftArray[0] + rightArray[0] + node.val;

        return result;
    }
}
